//
//  TKYUpdateProgressImageView.m
//  ProgressReportProject
//
//  Created by apple on 2018/4/23.
//  Copyright © 2018年 铁科院. All rights reserved.
//

#import "TKYUpdateProgressImageView.h"
#import "TKYRemarkImageCell.h"
@implementation TKYUpdateProgressImageView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.collectionView];
        
    }
    return self;
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.docListModelS.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TKYRemarkImageCell *cell= [collectionView dequeueReusableCellWithReuseIdentifier:KRemarkImageCellID forIndexPath:indexPath];
    
    TKYDocListModel *docListModel = self.docListModelS[indexPath.row];
    NSData *imageData = [[NSData alloc]initWithBase64EncodedString:docListModel.docPath options:(NSDataBase64DecodingIgnoreUnknownCharacters)];
    NSLog(@"%ld", imageData.length);
    cell.imagePicV.image = [UIImage imageWithData:imageData];
    cell.deleteImageBlock = ^(UIButton *deleteImageButton) {
        //从资源数组中移除该数据
      //  TKYDocListModel *docListModel = self.docListModelS[indexPath.row];
        if (docListModel.ID.length > 0) {
            [self requestDelectedImage:docListModel];
        }

        [self.docListModelS removeObject:docListModel];
        [self.collectionView reloadData];
    };
    return cell;
}



/**
 删除服务器图片
 */
- (void)requestDelectedImage:(TKYDocListModel *)docListModel{
    NSString *url = [NSString stringWithFormat:@"%@%@", kApiBaseUrl,kdeleteImgUrl];
    NSDictionary *parameterDic = @{
                                   @"token":[[NSUserDefaults standardUserDefaults]objectForKey:kToken],
                                   @"id":docListModel.ID
                                   };
    [KHttpRequestTool POST:url parameters:parameterDic success:^(id response) {
        NSLog(@"%@",response);
        if ([response[@"code"] integerValue] == 0) {
            [SVProgressHUD showSVProgressImage:nil Duration:2.0 FontSize:16 status:@"删除图片成功"];
        }else{
            [SVProgressHUD showSVProgressImage:nil Duration:2.0 FontSize:16 status:response[@"msg"]];
        }
        
    } failure:^(NSError *error) {
        [SVProgressHUD showSVProgressImage:nil Duration:2.0 FontSize:16 status:@"网络错误, 请稍候再试"];
        NSLog(@"%@", error);
    }];
    
}

#pragma mark--懒加载
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.itemSize = CGSizeMake(kHeightScale *160, kWidthScale *160);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumLineSpacing = 2;
        //layout.sectionInset = UIEdgeInsetsMake(0, 20, 0, 20);
        UICollectionView *collectionView;
        collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kHeightScale *160) collectionViewLayout:layout];
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.backgroundColor = [UIColor whiteColor];
        collectionView.bounces = YES;
        //注册cell
        [collectionView registerNib:[UINib nibWithNibName:@"TKYRemarkImageCell" bundle:nil] forCellWithReuseIdentifier:KRemarkImageCellID];
        collectionView.scrollsToTop = NO;
        collectionView.showsVerticalScrollIndicator = NO;
        collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView = collectionView;
    }
    return _collectionView;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.seletetedCollectionCellBlock) {
        self.seletetedCollectionCellBlock(indexPath);
    }
}


@end
